<script setup lang='ts'>
  import { nextTick, reactive, ref } from 'vue';
  import { systemApi } from '@/api/controller';
  import { ToastSuccess, Toastloading } from '@/utils/public';
  const formLabelWidth = '120px'
  const dataForm = ref()
  const $emit = defineEmits(['updateList'])
  const action = reactive({
    dialogFormVisible:false,
    disabled:false,
    title:'',
  })
  interface formQuert {
    id:number
    type:number
    key:string
    value:string
    desc:string,
    category:string
  }
  let form = reactive({} as formQuert)
  const formRules = {
    type: [{ required: true, message: "请选择字段类型", trigger: "blur" }],
    key: [{ required: true, message: "请输入键", trigger: "blur" }],
    value: [{ required: true, message: "请输入值", trigger: "blur" }],
    desc: [{ required: true, message: "请输入描述", trigger: "blur" }],
  }
  const handleSubmit = ()=>{
    dataForm.value.validate(async(valid:any)=>{
      if(!valid) return
      const loading = Toastloading()
      let submitSet = false
      const { id } = form
      if(id) {
        await systemApi.updateSystems(id,form).then(()=>{
          submitSet = true
          ToastSuccess('编辑成功')
        }).catch(()=>{
          loading.close()
        })
      }else {
        await systemApi.addSystems(form).then(()=>{
          submitSet = true
          ToastSuccess('新增成功')
        }).catch(()=>{
          loading.close()
        })
      }
      if(submitSet) {
        loading.close()
        $emit('updateList')
        closeDialog()
      }
    })
  }
  const openDialog = (query:any={})=> {
    let { id=0, key='', type=0, value='', desc='', category='' } = query
    if(category==null) category=''
    if(id) {
      action.title = '编辑配置'
      Object.assign(form,{
        id, key, type, value, desc, category
      })
    } else {
      action.title = '新增配置'
    }
    action.dialogFormVisible = true
  }
  const closeDialog = ()=>{
    form = reactive({} as formQuert)
    nextTick(() => {
      dataForm.value.clearValidate();
    });
    action.dialogFormVisible = false
  }
  defineExpose({
    openDialog
  })
</script>

<template>
  <div>
    <el-dialog
      top="10vh"
      class="xm-dialog input-width"
      v-model="action.dialogFormVisible"
      :close-on-click-modal="action.disabled"
      :show-close="action.disabled"
      :close-on-press-escape="action.disabled"
      width="52%"
      :title="action.title">
      <el-form ref="dataForm" :model="form" :rules="formRules">
        <el-form-item :label-width="formLabelWidth" label="字段类型" prop="type">
          <el-select v-model="form.type">
            <el-option label="字符串" :value="0" />
            <el-option label="整型" :value="1" />
          </el-select>
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="分类" prop="category">
          <el-select v-model="form.category">
            <el-option label="全部" value=""></el-option>
            <el-option label="基础" value="基础"></el-option>
            <el-option label="支付" value="支付"></el-option>
            <el-option label="风控" value="风控"></el-option>
            <el-option label="其他" value="其他"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="键" prop="key" >
          <el-input v-model="form.key" placeholder="请输入键" />
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="值" prop="value" >
          <el-input
            v-model="form.value"
            :autosize="{ minRows: 3, maxRows: 3}"
            type="textarea"
            placeholder="请输入值"
          />
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="描述" prop="desc" >
          <el-input
            v-model="form.desc"
            :autosize="{ minRows: 3, maxRows: 3}"
            type="textarea"
            placeholder="请输入描述"
          />
        </el-form-item>
      </el-form>
      <template v-if="!action.disabled" #footer>
        <span class="dialog-footer">
          <el-button @click="closeDialog">取 消</el-button>
          <el-button type="primary" @click="handleSubmit">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
